# 先登陆
import time
from hashlib import md5

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select

from DButils import DButils

path = 'msedgedriver.exe'
browser = webdriver.Edge(path)
browser.get('http://localhost:90')
browser.maximize_window()

import hashlib


def zhuce(name, password):
    browser.find_element(By.XPATH, "//a[text()='还没有账号？去注册']").click()
    browser.find_element(By.XPATH, "//*[@id='username']").send_keys(name)
    browser.find_element(By.XPATH, "//*[@id='password']").send_keys(password)
    browser.find_element(By.XPATH, "//*[@id='repassword']").send_keys(password)
    browser.find_element(By.XPATH, "//*[@id='login_btn']").click()

    time.sleep(2)
    # MD5 加密密码
    md5 = hashlib.md5()
    md5.update(password.encode('utf-8'))
    password_md5 = md5.hexdigest()

    # 查询数据库是否注册成功
    db = DButils('localhost', 'root', 'root', 'finance')
    # 注意：字符串字段需加引号防止 SQL 语法错误
    select = db.Select(f"SELECT * FROM user WHERE username='{name}' AND password='{password_md5}'")

    if len(select) != 1:
        print('注册失败')
    else:
        print('注册成功')

    time.sleep(5)


def login(name, password):
    # 找到账号密码框
    browser.find_element(By.XPATH, '//*[@id="username"]').send_keys(name)
    browser.find_element(By.XPATH, '//*[@id="password"]').send_keys(password)
    # 点击登录按钮
    browser.find_element(By.XPATH, '//*[@id="login_btn"]').click()
    # 等待加载
    time.sleep(2)
    if browser.title == '后台管理系统' or browser.title == '个人理财系统':
        print('登录成功')
    else:
        print('登录失败')
        exit()
        # 点击管理理财产品
    browser.find_element(By.XPATH, "//a[text()='理财产品管理']").click()
    time.sleep(0.5)
    # 点击基金理财
    browser.find_element(By.XPATH, "//a[text()='基金理财']").click()
    time.sleep(0.5)
# 测试新增数据
def add():
    # 分别测试新增，修改，删除，翻页
    # 先点击新增按钮，再输入值，最后点击保存，然后连接数据库进行验证
    # 循环20次新增数据
    for i in range(20):
        browser.find_element(By.XPATH, "//*[@id='add_modal_btn']").click()
        time.sleep(0.5)
        #     分别填充数据的值
        browser.find_element(By.XPATH, "//*[@id='code_add_input']").send_keys('111111')
        browser.find_element(By.XPATH, "//*[@id='funddesc_add_input']").send_keys(str(i))
        select = browser.find_element(By.XPATH, "//*[@id='addModal']//select[contains(class, form-control)]")
        jijinleixing = Select(select)
        jijinleixing.select_by_visible_text('债券型')
        browser.find_element(By.XPATH, "//*[@id='dailygrowth_add_input']").send_keys(str(i))
        browser.find_element(By.XPATH, "//*[@id='leastmoney_add_input']").send_keys(str(i))
        browser.find_element(By.XPATH, "//*[@id='investerm_add_input']").send_keys(str(i))
        browser.find_element(By.XPATH, "//*[@id='profit_add_input']").send_keys(str(i))
        browser.find_element(By.XPATH, "//*[@id='save_btn']").click()
        time.sleep(2)
        print(f'第{i + 1}条数据新增成功')
    # 读取数据库验证插入数据是否成功,插入数据后应该一共39条
    db = DButils('127.0.0.1', 'root', 'root', 'finance')
    select = db.Select('select * from fund_product')
    if len(select) == 39:
        print('数据插入无误')
# 测试修改数据
def update():
    # 点击编辑按钮
    browser.find_element(By.XPATH,"//tbody//button[contains(class, btn) and contains(class, btn-primary) and contains(class, btn-sm) and contains(class, update_btn) and @update-id='3']").click()
    time.sleep(0.5)
#     依次修改数据
    jijinjiancheng = browser.find_element(By.XPATH, "//*[@id='funddesc_update_input']")
    jijinjiancheng.clear()
    jijinjiancheng.send_keys('111111')
    jijinleixing = browser.find_element(By.XPATH, "//*[@id='type_update_input']")
    select=Select(jijinleixing)
    select.select_by_visible_text('货币型')
    rizengzhanglv = browser.find_element(By.XPATH, "//*[@id='dailygrowth_update_input']")
    rizengzhanglv.clear()
    rizengzhanglv.send_keys('0.0001')
    qitoujine = browser.find_element(By.XPATH, "//*[@id='leastmoney_update_input']")
    qitoujine.clear()
    qitoujine.send_keys('1000')
    touziqixian = browser.find_element(By.XPATH, "//*[@id='investerm_update_input']")
    touziqixian.clear()
    touziqixian.send_keys('50')
    yuqishouyi=browser.find_element(By.XPATH,"//*[@id='profit_update_input']")
    yuqishouyi.clear()
    yuqishouyi.send_keys('100')
#   点击更新按钮
    browser.find_element(By.XPATH,"//*[@id='update_btn']").click()
    time.sleep(3)
    # 读取数据库验证修改是否成功
    db = DButils('127.0.0.1', 'root', 'root', 'finance')
    select = db.Select('select * from fund_product where code=143745')
    print(type(select))
#   验证读取出的这条数据是否和修改的数据一致
    if len(select) == 1:
        row = select[0]
        assert row[3] == '111111', '基金简称不一致'
        assert row[1] == 3, '基金类型不一致，应为货币型（3）'
        assert float(row[4]) == 0.0001, '日增长率不一致'
        assert float(row[5]) == 1000.0, '起投金额不一致'
        assert float(row[6]) == 100.0, '预期收益不一致'
        assert row[7] == '50', '投资期限不一致'
        print('数据修改验证通过')
    else:
        print('未找到指定 code=143745 的数据，或数据不唯一')
# 测试删除数据
def delete():
#   点击删除按钮
    browser.find_element(By.XPATH,"//button[contains(class, btn) and contains(class, btn-danger) and contains(class, "
                                  "btn-sm) and contains(class, delete_btn) and @delete-id='11']//span[contains(class, "
                                  "glyphicon) and contains(class, glyphicon-trash)]").click()
    time.sleep(1)
# 确定删除
    browser.find_element(By.XPATH,"//button[text()='确认']").click()
#     查询数据库判断是否删除成功
    db = DButils('127.0.0.1', 'root', 'root', 'finance')
    select = db.Select('select * from fund_product where code=222222')
    if len(select) == 0:
        print('删除成功')
    else:
        print('删除失败')
# 测试翻页

def fanye():
#    循环点击翻页按钮
    while True:
        try:
            browser.find_element(By.XPATH,"//a[@href and @aria-label='Next']").click()
        except:
            print('已经到最后一页了')
            break
# login(name='admin', password='123456')
# test_add()
# update()
# delete()
# fanye()
zhuce('neko','123456')
login('neko','123456')
